package net.zhengxinyun.performance.controller.login;

import net.zhengxinyun.performance.Util.MD5;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.UserFilter;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.login.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hyp1202 on 2018/12/17.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private LoginService loginService;
    private TokenService tokenService;
    private UserService userService;

    @Autowired
    public UserController(LoginService loginService, TokenService tokenService, UserService userService) {
        this.loginService = loginService;
        this.tokenService = tokenService;
        this.userService = userService;
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody UserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            try {
                String passWord = MD5.md5(param.userEntity.getUserPassword(),MD5.MD5_STR);
                param.userEntity.setUserPassword(passWord);
                int i = userService.insert(param.userEntity);
                if (i == 0){
                    return ResultUtils.getResults(Result.Status.ERROR,"添加失败，请重试");
                }else {
                    return ResultUtils.getSuccessResults();
                }
            }catch (Exception e){
                return ResultUtils.getResults(Result.Status.ERROR,"添加失败，请重试");
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody UserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            int i = 0;
            try {
                i = userService.update(param.userEntity);
            } catch (Exception e) {
                return ResultUtils.getResults(Result.Status.ERROR,"修改失败，请重试");
            }
            if (i == 0){
                return ResultUtils.getResults(Result.Status.ERROR,"修改失败，请重试");
            }else {
                return ResultUtils.getSuccessResults();
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody UserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            int i = userService.delete(param.userEntity);
            if (i == 0){
                return ResultUtils.getResults(Result.Status.ERROR,"删除失败，请重试");
            }else {
                return ResultUtils.getSuccessResults();
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody UserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            if (!"admin".equals(userEntity.getUserPermissions())){
                param.userFilter.setUserDepartment(userEntity.getUserDepartment());
            }
            HashMap<String, Object> map = new HashMap<>();
            Double pageTotal = 1.0;
            if (!"agent".equals(userEntity.getUserPermissions())) {
                List<UserFilter> list = userService.query(param.userFilter);
                if (list.size() == 0) {
                    param.userFilter.setPageNumber(param.userFilter.getPageNumber() - 1);
                    list = userService.query(param.userFilter);
                }
                map.put("list",list);
                pageTotal = Util.intGetTotalPage(userService.queryForCount(param.userFilter), param.userFilter.getPageSize());
            }else {
                List<UserEntity> list = new ArrayList<>();
                list.add(userEntity);
                map.put("list",list);
            }
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对",null);
    }
}
class UserParam{
    public TokenEntity tokenEntity;
    public UserEntity userEntity;
    public UserFilter userFilter;
}
